﻿package com.widgets
{
	import flash.display.Sprite;
	
	import com.components.*;
	
	import com.components.PlayButton;
	import com.components.PauseButton;
	import com.components.StopButton;
	import com.components.NextButton;
	import com.components.PreviousButton;
	
	import com.conf.SkinClassReferencer;
	import com.conf.PlayerSpec;
	
	import com.utils.UICreatorUtils;
	
	import flash.events.MouseEvent;
	
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	// ButtonBar ::
	// - widget particulier qui contient l'ensemble des boutons de lecture du player
	/////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	public class ButtonBar extends Sprite
	{
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		// MEMBRES DE CLASSE
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		private var _btnPlay 		: PlayButton;
		private var _btnPause	 	: PauseButton;
		private var _btnStop		: StopButton;
		private var _btnNext		: NextButton;
		private var _btnPrevious	: PreviousButton;
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		// CONSTRUCTEUR
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		function ButtonBar ( )
		{
			super();
			
			init(  );
			
			btnPlay.addEventListener( MouseEvent.CLICK, handleMouseActions );
			btnPause.addEventListener( MouseEvent.CLICK, handleMouseActions );
			btnStop.addEventListener( MouseEvent.CLICK, handleMouseActions );
			btnNext.addEventListener( MouseEvent.CLICK, handleMouseActions );
			btnPrevious.addEventListener( MouseEvent.CLICK, handleMouseActions );
		}
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		// METHODE PROTEGEE : initialisation du widget
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		protected function init( ) : void
		{
			btnPlay 	= UICreatorUtils.spriteCreator( 
												  	SkinClassReferencer.PLAYBUTTON_TYPE,
													PlayerSpec.BTN_PLAY
													);
			btnPause	= UICreatorUtils.spriteCreator( 
												  	SkinClassReferencer.PAUSEBUTTON_TYPE,
													PlayerSpec.BTN_PAUSE
													);
			btnStop		= UICreatorUtils.spriteCreator( 
												  	SkinClassReferencer.STOPBUTTON_TYPE,
													PlayerSpec.BTN_STOP
													);
			btnNext		= UICreatorUtils.spriteCreator( 
												  	SkinClassReferencer.NEXTBUTTON_TYPE,
													PlayerSpec.BTN_NEXT
													);
			btnPrevious	= UICreatorUtils.spriteCreator( 
												  	SkinClassReferencer.PREVIOUSBUTTON_TYPE,
													PlayerSpec.BTN_PREVIOUS
													);
			trace("l'init interne du widget ButtonBar c'est bon");
			addChild( btnPlay );
			addChild( btnPause );
			addChild( btnStop );
			addChild( btnNext );
			addChild( btnPrevious );
		}
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		// METHODES PRIVEES : mécanismes internes du widget 
		// -> gestion de l'interaction entre les états des boutons
		// -> grâce à la diffusion d'un MouseEvent personnalisé
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		// on rajoute des interactions spécifiques pour les états des composants
		private function handleMouseActions( pEvt : MouseEvent ) : void
		{
			switch( pEvt.type )
			{
				case MouseEvent.CLICK:
				if ( pEvt.target is PlayButton )
				{
					if ( ! btnPause.isSelected )
						btnPause.isActive = true;
					else
						dispatchWidgetEvent( 
											PlayerSpec.BTN_PAUSE,
											PlayerSpec.WIDGET_BUTTON_STATE_CHANGE_UP );
						
				}
				else if ( pEvt.target is PauseButton )
				{
					if ( btnPlay.isSelected )
						dispatchWidgetEvent( 
											PlayerSpec.BTN_PLAY,
											PlayerSpec.WIDGET_BUTTON_STATE_CHANGE_UP
											);
					else if ( btnPause.isActive )
						dispatchWidgetEvent( 
											PlayerSpec.BTN_PLAY,
											PlayerSpec.WIDGET_BUTTON_STATE_CHANGE_HIT_TEST
											);
				}
				else if ( pEvt.target is StopButton )
				{
					if ( btnPlay.isSelected )
						dispatchWidgetEvent( 
											PlayerSpec.BTN_PLAY,
											PlayerSpec.WIDGET_BUTTON_STATE_CHANGE_UP
											);
					if ( btnPause.isSelected )
						dispatchWidgetEvent( 
											PlayerSpec.BTN_PAUSE,
											PlayerSpec.WIDGET_BUTTON_STATE_CHANGE_UP
											);
					btnPause.isActive = false;
				}
				else if ( ( pEvt.target is NextButton ) || ( pEvt.target is PreviousButton ) )
				{
					if ( btnPause.isSelected )
					{
						dispatchWidgetEvent( 
											PlayerSpec.BTN_PAUSE,
											PlayerSpec.WIDGET_BUTTON_STATE_CHANGE_UP
											);
						dispatchWidgetEvent( 
											PlayerSpec.BTN_PLAY,
											PlayerSpec.WIDGET_BUTTON_STATE_CHANGE_HIT_TEST
											);
					}
				}
				break;
				
				default:
				// on ne gère que les événements CLIC pour les interactions spécifiques entre les etats des boutons
				break;
			}
		}
		
		// on dispatche sur le composant un événement MouseEvent personnalisé
		private function dispatchWidgetEvent ( pComponent : String, pEvt : String ) : void
		{
			( ( this.getChildByName( pComponent ) ) as PlayerButton ).dispatchEvent( new MouseEvent( pEvt ) );
		}
		
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		// GETTER & SETTER
		/////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		public function get btnPlay( ) : PlayButton
		{
			return _btnPlay;
		}
		
		public function set btnPlay ( val : PlayButton ) : void
		{
			_btnPlay = val;
		}
		
		public function get btnPause ( ) : PauseButton
		{
			return _btnPause;
		}
		
		public function set btnPause ( val : PauseButton ) : void
		{
			_btnPause = val;
		}
		
		public function get btnStop ( ) : StopButton
		{
			return _btnStop;
		}
		
		public function set btnStop ( val : StopButton ) : void
		{
			_btnStop = val;
		}
		
		public function get btnNext ( ) : NextButton
		{
			return _btnNext;
		}
		
		public function set btnNext ( val : NextButton ) : void
		{
			_btnNext = val;
		}
		
		public function get btnPrevious ( ) : PreviousButton
		{
			return _btnPrevious;
		}
		
		public function set btnPrevious ( val : PreviousButton ) : void
		{
			_btnPrevious = val;
		}
	}
}